import {
	MOCK_USER_DATA
} from "@/utils/constant.js"
import request from "../request.js"
import {
	getCustomerBaseInfo
} from "./customerBaseAPI.js"
// 发送短信验证码
export function sendSmsCode(data, enableLoading = true) {
	return request.post({
		url: "/duzikai_api_customer_public_global_mgr",
		data: {
			invoke_method: 'send_msg_phone',
			...data,
			pre_cmd_para_db_exist_user: false,
			pre_cmd_para_need_pic_code: false,
		},
		notReturnData: true,
		resetCookie: true,
		enableLoading
	});
}
// 用户手机号登录
export const mobileLogin = (data, enableLoading = true) => {
	return request.post({
		url: '/duzikai_api_customer_public_global_mgr',
		data: {
			invoke_method: 'login_by_phone_code',
			...data,
			// pre_cmd_para_need_pic_code: false
		},
		resetCookie: true,
		enableLoading
	})
}
/**
 * 用户微信登录
 * @param {String}  data.code
 */
export const weixinLogin = (data, enableLoading = true) => {
	return request.post({
		url: '/duzikai_api_weichat_enter_4_app_wholefarm',
		data: {
			invoke_method: 'app_acquire_user_info',
			id_public_data_admin_user: 800,
			app_type: 4,
			...data,
			// pre_cmd_para_need_pic_code: false
		},
		resetCookie: true,
		enableLoading
	})
}

export const appleLogin = (data, enableLoading = true) => {
	return request.post({
		url: '/duzikai_api_other_platform_enter_4_app_wholefarm',
		data: {
			invoke_method: 'register_or_enter',
			id_public_data_admin_user: 800,
			// app_type:4,
			...data,
			// pre_cmd_para_need_pic_code: false
		},
		resetCookie: true,
		enableLoading
	})
}

/**
 * 微信小程序登录
 * @param {String}  data.code
 */
const getMpWeixinUserInfo = async () => {
	let userInfo = await getCustomerBaseInfo({
		invoke_method: 'user_and_shop_info'
	}, false).catch((err) => {
		return Promise.reject(err)
	})
	if (userInfo) {
		return Promise.resolve(userInfo)
	}
}

// 首次登录需要解析用户信息
const decodeMpWeixinUserInfo = async () => {
	let resultDecode = await request.post({
		url: '/duzikai_api_lite_program_enter_4_app_wholefarm',
		data: {
			invoke_method: 'lite_pro_decode_user_info',
		},
		enableLoading: false,
		notReturnData: true
	}).catch((err) => {
		return Promise.reject(err)
	})
	if (resultDecode) {
		let resultInfo = await getMpWeixinUserInfo().catch(err => {
			return Promise.reject(err)
		})
		if (resultInfo) {
			return Promise.resolve(resultInfo)
		}
		
	}


}
export const mpWeixinLoginJump = async () => {
	let url = ""
	let jumpData = {
		url: '/duzikai_api_customer_public_global_mgr',
		data: {
			invoke_method: 'public_page_4a_login_jump',
			pre_cmd_para_pass_way_type: 'lite_pro',
			jump_type: 999,
		},
		resetCookie: true,
		notReturnData: true
	}
	let jumpResult = await request.post(jumpData).catch((err) => {
		console.log(err, ">>>>")
		// return Promise.reject(err)
	})
	console.log(jumpResult, ">>>>")
	if (jumpResult) {
		return Promise.resolve(jumpResult)
	} else {
		return Promise.reject(err)
	}
}
export const getSettingDecodeUserInfo=()=>{
	return new Promise((resolve,reject)=>{
		uni.getSetting({
		success: async () => {
			let loginUserInfo = await decodeMpWeixinUserInfo().catch(err => {
				console.log(err, 'loginStep1>>>>err>>>decodeMpWeixin')
				// reject(err)
			})
			console.log(loginUserInfo, 'loginStep1>>>>success>>>decodeMpWeixin')
			if (loginUserInfo) {
				return resolve(loginUserInfo)
			}
			else{
				return reject({code:500,msg:'登录失败'})
			}
		}
	})
	})
	
}
export const mpWeixinLogin = async (data, enableLoading = true) => {

	// let jumpResult = await mpWeixinLoginJump().catch(err => {
	// 	console.log(err);
	// 	return Promise.reject(err);
	// })
	// console.log(jumpResult)
	// if (!jumpResult) {
	// 	return Promise.reject(jumpResult)
	// }

	let url = '/duzikai_api_lite_program_enter_4_app_wholefarm'
	let postData = {
		...data
	}
	let loginStep1 = await request.post({
		url,
		data: postData,
		enableLoading,
		resetCookie: true,
	}).catch((err) => {
		return Promise.reject(err)
	})
	if (loginStep1) {
		if (loginStep1.exist_user_info) {
			let loginUserInfo = await getMpWeixinUserInfo().catch(err => {
				return Promise.reject(err)
			})
			if (loginUserInfo) {
				return Promise.resolve(loginUserInfo)
			}
		} else {
			
			return getSettingDecodeUserInfo();
		}

	}

}

// mockLogin
export const mockUserLogin = async (enableLoading = true) => {
	return request.post({
		...MOCK_USER_DATA,
		enableLoading,
		resetCookie: true
	});
}